﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Business.Stores;
using MyCommerce.Business.GeneralParameters;
using Innova.Microfinanzas.BusinessEntities.EnumClassifierTypes;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;

namespace MyCommerce.Presentation.Stores.Presenters
{
    public class SalesChargePresenter
    {
        private IViews.ISalesCharge vManagedView;

        public SalesChargePresenter(IViews.ISalesCharge managedView)
        {
            if (managedView == null)
            {
                throw new Exception("managedView cannot be null");
            }

            vManagedView = managedView;
            vManagedView.OnLoadView += new Innova.FoundationFramework.DataTypes.GenericEventHandler<string>(vManagedView_OnLoadView);
            vManagedView.OnChangeMoney += new Innova.FoundationFramework.DataTypes.GenericEventHandler<decimal>(vManagedView_OnChangeMoney);
            vManagedView.OnSave += new EventHandler(vManagedView_OnSave);
            vManagedView.OnCancel += new EventHandler(vManagedView_OnCancel);

        }

        void vManagedView_OnChangeMoney(object sender, Innova.FoundationFramework.DataTypes.GenericEventArgs<decimal> e)
        {
            CurrencyExchangeManager exchangeMgr = new CurrencyExchangeManager();
            CurrencyExchange objExchange = new CurrencyExchange();
            objExchange.IdMoneyIncome = Convert.ToInt64(MoneyEnum.ExternalMoney);
            objExchange.IdMoneyExit = Convert.ToInt64(MoneyEnum.LocalMoney);
            objExchange.AmmountIncome = e.EventData;
            CurrencyExchange objExchangeRes = exchangeMgr.CurrencyExchangeGetExchange(objExchange);
            if (objExchangeRes != null)
            {
                vManagedView.RateExchange = objExchangeRes.CurrencyExchangeRate;
                vManagedView.SetRealAmount(objExchangeRes.AmmountExit, MoneyEnum.LocalMoney);
            }
        }

        void vManagedView_OnLoadView(object sender, Innova.FoundationFramework.DataTypes.GenericEventArgs<string> e)
        {
        }

        void vManagedView_OnSave(object sender, EventArgs e)
        {
            SalesOrderManager SalesOrderMgr = new SalesOrderManager(true);
            SalesOrderMgr.SaveOrderDetail(vManagedView.ObjSalesOrder, vManagedView.ColSalesOrderDetail);
            vManagedView.CloseAndSetResult(true);
        }

        void vManagedView_OnCancel(object sender, EventArgs e)
        {
            vManagedView.CloseAndSetResult(false);
        }


    }
}
